﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from Entities import *
#---------------------------
# Entities TinhThanh
#---------------------------
def Get(keyNguoiDung, TinhThanhID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    tinhthanh_list = []
    if TinhThanhID > 0:
        tinhthanh = TinhThanh.get_by_id(TinhThanhID)
        tinhthanh_dict ={}
        tinhthanh_dict['TinhThanhID'] = tinhthanh.key().id()
        tinhthanh_dict['TenTinhThanh'] = tinhthanh.TenTinhThanh   
        tinhthanh_list.append(tinhthanh_dict)
    else:
        tinhthanhs = TinhThanh.all()
        for tinhthanh in tinhthanhs:
            tinhthanh_dict ={}
            tinhthanh_dict['TinhThanhID'] = tinhthanh.key().id()
            tinhthanh_dict['TenTinhThanh'] = tinhthanh.TenTinhThanh   
            tinhthanh_list.append(tinhthanh_dict)
    tuple_tinhthanh = tuple(tinhthanh_list)  
    return tuple_tinhthanh

def Add(TenTinhThanh):
    tinhthanh = db.GqlQuery("SELECT * FROM TinhThanh WHERE TenTinhThanh = :1",
                              TenTinhThanh)
    if tinhthanh.count() == 0:                      
        tinhthanh = TinhThanh(TenTinhThanh=TenTinhThanh)
        tinhthanh.put()         
        return "Complete"
    else:
        return "Duplicate"

def Update(TinhThanhID, TenTinhThanh):
    tinhthanh_update = TinhThanh.get_by_id(LinhVucID)
    tinhthanh = db.GqlQuery("SELECT * FROM TinhThanh WHERE TenTinhThanh = :1",
                              TenTinhThanh)
    if tinhthanh.count() == 0:
        tinhthanh_update.TenTinhThanh = TenTinhThanh
        tinhthanh_update.put()
        return "Complete"
    else:
        return "Duplicate"
